package com.flea.hclv.controller;

import com.alibaba.fastjson.JSON;
import com.flea.hclv.model.entity.HResourceRelation;
import com.flea.hclv.model.entity.HVisitor;
import com.flea.hclv.service.HVisitorService;
import com.flea.hclv.util.ServerResponse;
import com.flea.system.bean.FleaPage;
import com.flea.system.entity.OrgDept;
import com.flea.system.platform.session.FleaOrgUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @program: project
 * @description: 游客
 * @author: shiliqiang
 * @create: 2018-09-26 20:44
 * @version: 1.0
 */
@Controller
@RequestMapping("/flea/hclv/visitor/")
public class HVisitorController {

    @Autowired
    private HVisitorService hVisitorService;

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public ServerResponse<String> saveOrUpdate(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String userId = FleaOrgUtils.getCurUserUuid(request);
        OrgDept currentUserDept = FleaOrgUtils.getCurrentUserDept(request);
        String model = request.getParameter("model");
        String image = JSON.parseObject(model).getString("images");
        List<HResourceRelation> hResourceRelations = JSON.parseArray(image,HResourceRelation.class);
        HVisitor visitor = JSON.parseObject(model,HVisitor.class);
        ServerResponse<String> serverResponse = hVisitorService.saveOrUpdate(userId,currentUserDept,visitor,hResourceRelations);
        return serverResponse;
    }
    /**
     * 查找所有  （供管理员使用）
     * @param request request
     * @param paging 分页对象
     * @param zhName 中文名字
     * @param enName 英文名
     * @return list
     */
    @RequestMapping("selectAll")
    @ResponseBody
    public ServerResponse<FleaPage<HVisitor>> selectAll(HttpServletRequest request, FleaPage<HVisitor> paging,
                                                        @RequestParam(value = "zhName",defaultValue = "")String zhName,
                                                        @RequestParam(value = "enName",defaultValue = "")String enName){
        OrgDept currentUserDept = FleaOrgUtils.getCurrentUserDept(request);
        return hVisitorService.selectAll(currentUserDept,paging.getCurPage(),paging.getPageSize(),zhName,enName);

    }

    /**
     * 根据部门查找
     * @param request request
     * @param paging 分页对象
     * @param zhName 中文名字
     * @param enName 英文名
     * @return list
     */
    @RequestMapping("selectByDeptId")
    @ResponseBody
    public ServerResponse<FleaPage<HVisitor>> selectByDeptId(HttpServletRequest request, FleaPage<HVisitor> paging,
                                                        @RequestParam(value = "zhName",defaultValue = "")String zhName,
                                                        @RequestParam(value = "enName",defaultValue = "")String enName){
        OrgDept currentUserDept = FleaOrgUtils.getCurrentUserDept(request);
        return hVisitorService.selectByDeptId(currentUserDept,paging.getCurPage(),paging.getPageSize(),zhName,enName);

    }
    //订单添加游客的列表
    @RequestMapping("selectByDept")
    @ResponseBody
    public ServerResponse<FleaPage<HVisitor>> selectByDeptId(HttpServletRequest request,FleaPage<HVisitor> paging,
                                                             @RequestParam(value = "name",defaultValue = "")String zhName
                                                         ){
        OrgDept currentUserDept = FleaOrgUtils.getCurrentUserDept(request);
        return hVisitorService.selectByDept(currentUserDept,zhName,paging.getCurPage(),paging.getPageSize());

    }

    @RequestMapping("deleteByIds")
    @ResponseBody
    public ServerResponse<String> deleteByIds(HttpServletRequest request,HttpServletResponse response, @RequestParam("ids")String ids){
        String userId = FleaOrgUtils.getCurUserUuid(request);
        List<String> idList = JSON.parseArray(ids,String.class);
        ServerResponse<String> serverResponse =  hVisitorService.deleteByIds(userId,idList);
        return serverResponse;
    }
    @RequestMapping("selectByName")
    @ResponseBody
    public ServerResponse<List<HVisitor>> selectByName(@RequestParam("name")String visitorName,HttpServletRequest request){
        OrgDept currentUserDept = FleaOrgUtils.getCurrentUserDept(request);
        return hVisitorService.selectByName(currentUserDept,visitorName);
    }
    @RequestMapping("selectVisitorImage")
    @ResponseBody
    public ServerResponse<List<HResourceRelation>> selectVisitorImage(String visitorId){
        if (visitorId==null){
            return ServerResponse.createByErrorMessage("参数错误");
        }
        return hVisitorService.selectVisitorImage(visitorId);
    }
    @RequestMapping("addVisitor")
    @ResponseBody
    public ServerResponse<HVisitor> addVisitor(HttpServletRequest request) throws Exception {
        String userId = FleaOrgUtils.getCurUserUuid(request);
        OrgDept currentUserDept = FleaOrgUtils.getCurrentUserDept(request);
        String model = request.getParameter("model");
        String image = JSON.parseObject(model).getString("image");
        HResourceRelation hResourceRelation = JSON.parseObject(image,HResourceRelation.class);
        HVisitor visitor = JSON.parseObject(model,HVisitor.class);
        ServerResponse<HVisitor> hVisitorServerResponse = hVisitorService.save(userId,currentUserDept,visitor,hResourceRelation);
        return hVisitorServerResponse;
    }
    @RequestMapping("selectByIdsList")
    @ResponseBody
    public ServerResponse<List<HVisitor>> selectByIdsList(String idList){
        return hVisitorService.selectByIdsList(idList);
    }
    @RequestMapping("selectByVisitorId")
    @ResponseBody
    public ServerResponse<HVisitor> selectByVisitorId(String visitorId){
        return hVisitorService.selectByVisitorId(visitorId);
    }

}
